package utils

import (
	"encoding/json"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	"time"
	"wx_build/core/logs"
)

func ToMap(data interface{}) (map[string]interface{}, error) {
	m := make(map[string]interface{})
	j, _ := json.Marshal(data)
	err := json.Unmarshal(j, &m)
	if err != nil {
		logs.Err("tomap转化失败", err)
		return nil, err
	}
	createTime, _ := time.Parse(time.RFC3339, m["CreatedAt"].(string))
	updateTime, _ := time.Parse(time.RFC3339, m["UpdatedAt"].(string))
	m["create_time"] = createTime.Format("2006-01-02 15:04:05")
	m["update_time"] = updateTime.Format("2006-01-02 15:04:05")
	//time.Parse("2006-01-02 15:04:05", string(timeStr))
	//m["create_time"] = m["CreatedAt"]
	//m["update_time"] = m["UpdatedAt"]
	m["id"] = m["ID"]
	delete(m, "password")
	delete(m, "Password")
	delete(m, "CreatedAt")
	delete(m, "UpdatedAt")
	delete(m, "ID")
	delete(m, "DeletedAt")
	return m, err
}
func GetConfig(key string) *g.Var {
	var ctx = gctx.New()
	val, err := g.Cfg().Get(ctx, key)
	if err != nil {
		return nil
	}
	return val
}
